package com.daxidi.cn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxidi.cn.entity.User;
import com.daxidi.cn.mapper.UserMapper;
import com.daxidi.cn.service.IClerkService;
import com.daxidi.cn.service.IManagerService;
import com.daxidi.cn.service.IMemberService;
import com.daxidi.cn.utils.RedisUtils;
import com.daxidi.cn.utils.ReturnResult;
import com.daxidi.cn.utils.ReturnResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JiaLin
 * create 2020-11-18
 */
@Log4j
@Api(tags = "注销 退出登录接口")
@RequestMapping(value = "/api/v1/unions/logout")
@RestController
public class LogoutController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IManagerService managerService;

    @Autowired
    private IClerkService clerkService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "用户注销 退出登录")
    @GetMapping(value = "/member/{memberId}")
    public ReturnResult logoutMember(@PathVariable Long memberId, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //redisUtils.del(token);
            //置空openId
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("membe_id", memberId);
            User user = userMapper.selectOne(wrapper);
            user.setOpenId("-1");
            userMapper.updateById(user);
            return ReturnResultUtils.returnSuccess("注销成功！");
        } catch (Exception e) {
            log.error("注销失败！" + e);
            e.printStackTrace();
            return ReturnResultUtils.returnFailMsg(314, "注销失败！");
        }
    }

    @ApiOperation(value = "店员注销 退出登录")
    @GetMapping(value = "/clerk/{clerkId}")
    public ReturnResult logoutClerk(@PathVariable Long clerkId, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //redisUtils.del(token);
            //置空openId
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("clerk_id", clerkId);
            User user = userMapper.selectOne(wrapper);
            user.setOpenId("-1");
            userMapper.updateById(user);
            return ReturnResultUtils.returnSuccess("注销成功！" + clerkService.selectClerkById(clerkId).getName());
        } catch (Exception e) {
            log.error("注销失败！" + e);
            e.printStackTrace();
            return ReturnResultUtils.returnFailMsg(314, "注销失败！");
        }
    }


    @ApiOperation(value = "管理员注销 退出登录")
    @GetMapping(value = "/manager/{managerId}")
    public ReturnResult logoutManager(@PathVariable Long managerId, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //redisUtils.del(token);
            return ReturnResultUtils.returnSuccess("注销成功！" + managerService.selectManagerById(managerId).getUsername());
        } catch (Exception e) {
            log.error("注销失败！" + e);
            e.printStackTrace();
            return ReturnResultUtils.returnFailMsg(314, "注销失败！");
        }
    }

}
